package LC;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LC139
 * @Description: TODO
 * @Author hl
 * @Date 2020/6/8
 * @Version V1.0
 **/
public class LC139 {

    public static void main(String[] args) {
        List<String> wordDict=new ArrayList<>();
        wordDict.add("leet");
        wordDict.add("code");

        System.out.println(wordBreak("leetcode",wordDict));
    }


    public static boolean wordBreak(String s, List<String> wordDict) {

        boolean[] check=new boolean[s.length()+1];
        check[0]=true;
        int maxLen=0;
        for(String key:wordDict){
            if(key.length()>maxLen){
                maxLen=key.length();
            }
        }

        for(int i=1;i<check.length;i++){
            for(int j=i-1;j>=0&&(i-j)<=maxLen;j--){
                if(check[j]==true&&wordDict.contains(s.substring(j,i))){
                    check[i]=true;
                }
            }
        }
        return check[s.length()];
    }
}
